﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using EvidenceLib.System;
using EvidenceService.Core;
using NHibernate;

namespace EvidenceService.System
{
    public class UserService : BasicService<User>
    {
        public UserService(ISession session)
            : base(session)
        {

        }

        public virtual User LoadByNameAndPwd(string name, string pwd)
        {
            User user = null;

            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(pwd))
            {
                try
                {
                    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                    string md5_password = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(pwd))).Replace("-", "");
                    string hql = string.Format("from User as u where u.Number='{0}' and u.Password='{1}'", name, md5_password);
                    user = mSession.CreateQuery(hql).UniqueResult<User>();
                }
                catch (Exception ex)
                {

                }
            }
            return user;
        }
    }
}
